package com.yinhai.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.androidquery.AQuery;
import com.yinhai.activity.MoreAppADContentActivity;
import com.yinhai.activity.R;
import com.yinhai.model.MoreAppAd;

public class MoreAppAdListAdapter extends BaseAdapter {
	private ArrayList<MoreAppAd> dataList;
	private LayoutInflater layoutInflater;
	private Context context;
	
	public MoreAppAdListAdapter(Context context){
		this.context=context;
		this.dataList=new ArrayList<MoreAppAd>();
		this.layoutInflater=LayoutInflater.from(this.context);
	}
	
	public void addItem(MoreAppAd item){
		dataList.add(item);
	}
	
	public void clear(){
		dataList.clear();
	}
	
	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public MoreAppAd getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView==null) {
			convertView = layoutInflater.inflate(R.layout.more_app_list_item, null);
		}
		AQuery aq = new AQuery(convertView);
		MoreAppAd u = getItem(position);
		aq.id(R.id.layad).tag(u).clicked(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent it = new Intent(context, MoreAppADContentActivity.class);
				MoreAppAd maa = (MoreAppAd)v.getTag();
				String detail = maa.getDetail();
				String icon = maa.getIcon();
				it.putExtra("detail", detail);
				it.putExtra("icon", icon);
				context.startActivity(it);
				/*Intent intent = new Intent(); 
				//v.getTag()
				intent.setAction("android.intent.action.VIEW");    
				Uri content_url = Uri.parse(v.getTag().toString());   
				intent.setData(content_url);  
				context.startActivity(intent);*/
			}
		});
		
		aq.id(R.id.ad_small_icon).image(u.getSmall_icon(),true,true);
		aq.id(R.id.ad_title).text(u.getTitle());
		aq.id(R.id.ad_short_detail).text(u.getShort_detail());
		return convertView;
	}
}
